package com.vr.xuecheng.learning.controller;

import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.vr.xuecheng.base.exception.XueChengPlusException;
import com.vr.xuecheng.common.util.SecurityUtils;
import com.vr.xuecheng.learning.dto.CourseTablesDTO;
import com.vr.xuecheng.learning.service.IChooseCourseService;
import com.vr.xuecheng.learning.vo.ChooseCourseVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* <p>
*  前端控制器
* </p>
*
* @author hzh
*/
@Slf4j
@RestController
@Tag(name = "选择课程控制器")
@ApiSupport(author = "hzh", order = 1)
@RequestMapping("/choosecourse")
@RequiredArgsConstructor
public class ChooseCourseController {
    private final IChooseCourseService chooseCourseService;
    @Operation(summary = "添加选课课程")
    @PostMapping("/{course-id}")
    public ChooseCourseVO chooseCourse(@PathVariable("course-id") Long courseId) {
        // 获取当前登录的用户id
        return chooseCourseService.chooseCourse(courseId);
    }

    @Operation(summary = "获取学习资格")
    @PostMapping("/learnstatus/{course-id}")
    public CourseTablesDTO getLearningStatus(@PathVariable("course-id") Long courseId) {
        // 获取当前登录的用户id
        SecurityUtils.User user = SecurityUtils.getUser();
        if (user == null) {
            log.error("用户未登录,无法获取学习资格");
            XueChengPlusException.cast("用户未登录,无法获取学习资格");
        }
        return chooseCourseService.getLearningStatus(user.getId(), courseId);
    }

}
